package com.hl233.framework.nettyServer;

import io.netty.channel.Channel;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 存储通道的标识和通道对象
 * @author 张振强15065206763
 * @create 2024-09-07
 */
public class ChannelHandlerContextMap {

    private static ChannelHandlerContextMap instance;
    private final Map<String, Channel> channelMap;

    private ChannelHandlerContextMap() {
        channelMap = new ConcurrentHashMap<>();
    }

    public static synchronized ChannelHandlerContextMap getInstance() {
        if (instance == null) {
            instance = new ChannelHandlerContextMap();
        }
        return instance;
    }

    public void put(String identifier, Channel ctx) {
        channelMap.put(identifier, ctx);
    }

    public Channel get(String identifier) {
        return channelMap.get(identifier);
    }

    public Channel remove(String identifier) {
        return channelMap.remove(identifier);
    }

    public Boolean containsKey(String identifier) {
        return channelMap.containsKey(identifier);
    }

    public Set<Map.Entry<String, Channel>> entrySet() {
        return channelMap.entrySet();
    }

    public int size() {
        return channelMap.size();
    }
}
